﻿using ERP.ERPSystem.ProductionSystem;
using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;
using ERP.Team.RBAC;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ERP.Web.Controllers.ProductionSystem
{
    /// <summary>
    /// 生产系统-质量管理-出货检验-xzc
    /// </summary>
    public class OutgoingInspectionController : Controller
    {
        private readonly IOutgoingInspectionServices _outgoingInspectionServices;

        public OutgoingInspectionController(IOutgoingInspectionServices outgoingInspectionServices)
        {
            _outgoingInspectionServices = outgoingInspectionServices;
        }

        /// <summary>
        /// 出货检验视图
        /// </summary>
        /// <returns></returns>
        public IActionResult InspectionView()
        {
            return View();
        }
        /// <summary>
        /// 新增出货检验视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddInspectionView()
        {
            return View();
        }
        /// <summary>
        /// 新增出货检验
        /// </summary>
        /// <param name="outgoinginspectiondto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddOutgoingInspection(OutgoingInspectionDto outgoinginspectiondto)
        {
            var i = await _outgoingInspectionServices.AddOutgoingInspection(outgoinginspectiondto);
            return i;
        }
        /// <summary>
        /// 更新出货检验视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateInspectionView()
        {
            return View();
        }
        /// <summary>
        /// 通过id查询到出货检验数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<OutgoingInspection>> FindOutgoingInspection(int id)
        {
            var i = await _outgoingInspectionServices.FindOutgoingInspection(id);
            return i;
        }
        /// <summary>
        /// 更新出货检验
        /// </summary>
        /// <param name="outgoinginspectiondto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateOutgoingInspection(OutgoingInspectionDto outgoinginspectiondto)
        {
            var i = await _outgoingInspectionServices.UpdateOutgoingInspection(outgoinginspectiondto);
            return i;
        }
        /// <summary>
        /// 删除出货检验
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeleteOutgoingInspection(int id)
        {
            await _outgoingInspectionServices.DeleteOutgoingInspection(id);
        }
        /// <summary>
        /// 批量删除出货检验
        /// </summary>
        /// <param name="ids">要删除的ID列表(逗号分隔)</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult BatchDeleteOutgoingInspection(string ids)
        {
            try
            {
                if (string.IsNullOrEmpty(ids))
                {
                    return Json(new { success = false, message = "请选择要删除的记录" });
                }

                // 解析ID字符串为ID列表
                var idList = ids.Split(',')
                               .Where(x => !string.IsNullOrWhiteSpace(x))
                               .Select(int.Parse)
                               .ToList();

                var result = _outgoingInspectionServices.BatchDeleteOutgoingInspection(idList).Result;
                
                return Json(new { success = true, count = result, message = "删除成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "删除失败：" + ex.Message });
            }
        }
        /// <summary>
        /// 查询出货检验
        /// </summary>
        /// <param name="getoutgoinginspectiondto"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<OutgoingInspection>> GetOutgoingInspection(GetOutgoingInspectionDto getoutgoinginspectiondto)
        {
            return await _outgoingInspectionServices.GetOutgoingInspection(getoutgoinginspectiondto);
        }
        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <returns></returns>
        public IActionResult GetUserListView()
        {
            return View();
        }
        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<UserModel>> GetUserList(GetUserListDto getuserlistdto)
        {
            return await _outgoingInspectionServices.GetUserList(getuserlistdto);
        }
    }
}
